//
//  ViewController.swift
//  Swift3
//
//  Created by wangxiaobo on 16/7/6.
//  Copyright © 2016年 com.lovewith.lovewith. All rights reserved.
//

import UIKit

class ItemCell: UITableViewCell {
    @IBOutlet weak var title: UILabel!
}

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    func greet(_ person: String, _ day: String) {
        print("Hello, \(person), this day is \(day).")
    }

    func sum(numbers: Int...) -> Int {
        var sum = 0
        for num in numbers{
            sum += num
        }
        return sum
    }

    func fib(n: Int) -> Int {
        if n <= 2{
            return 1;
        }else{
            return fib(n: n-1) + fib(n: n-2);
        }
    }

    func addRandom(num: Int, add: (Int)->Int) -> Int{
        return add(num);
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "分享", style: .plain, target: self, action: #selector(ViewController.shareAction) )

        greet("name", "thursday");
        print(sum(numbers: 1,2,3));
        print(fib(n: 8));
        print( addRandom(num: 3) { n -> Int in return n + 7 } )
        print( addRandom(num: 3) { n in n + 7 } )
    }

    // MARK:-数据懒加载
    lazy var dataArray = {() -> [String] in

        return ["The Basics",
                "Operation",
                "String And Characters",
                "Collection Types",
                "ControlFlow",
                "Functions",
                "Closures",
                "Enumerations",
                "Class And Strutures",
                "Properties",
                "Methods",
                "Subscripts",
                "Inheritance",
                "Initialization",
                "Deinitialization",
                "ARC",
                "Optional Chaining",
                "Error Handling",
                "Type Casting",
                "Nested Types",
                "Extensions",
                "Protocols",
                "Generics",
                "Access Control",
                "Advanced Operators"]
    }()

    // MARK:分享
    func shareAction() {
        let req = SendMessageToWXReq()
        req.text = "Hello"
        req.bText = true
        req.scene = 0;
        WXApi.send(req);
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension ViewController: UITableViewDelegate,UITableViewDataSource{

    //MARK: - UITableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell")
        cell?.textLabel?.text = dataArray[(indexPath as NSIndexPath).row]
        cell?.imageView?.image = UIImage(named: "Logo")
        return cell!;
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44;
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true);
        self.performSegue(withIdentifier: "goContent", sender: (indexPath as NSIndexPath).row )
    }

    func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier != "goDraw"{
            let vc = segue.destination as! ContentViewController
            vc.contentType = ContentType(rawValue: sender as! Int )!
        }
    }
}

extension ViewController: UITextFieldDelegate{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        return true
    }
}
